import requests

from api import log
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from tool import common_assert, html_parser


class TestApproveTrust:
    def setup_method(self):
        self.session = requests.session()
        self.api = ApiApproveTrust(self.session)
        ApiRegisterLogin(self.session).api_login()

    def teardown_method(self):
        self.session.close()

    def test01_approve(self, description="提交成功"):
        try:
            result = self.api.api_approve()
            log.info("认证接口执行结果为:{}".format(result.text))
            common_assert(result, description=description)
            log.info("认证接口断言通过！")
        except Exception as e:
            log.error(e)
            raise

    def test02_approve_status(self, text="7712"):
        try:
            result = self.api.api_approve_status()
            log.info("查询认证状态证接口执行结果为:{}".format(result.text))
            common_assert(result, text=text)
            log.info("查询认证状态证接口断言通过！")
        except Exception as e:
            log.error(e)
            raise

    def test03_trust(self, text="form", text2="OK"):
        try:
            result = self.api.api_trust()
            print("后台开户结果为:{}".format(result.json()))
            log.info("后台开户接口执行结果为:{}".format(result.json()))
            common_assert(result, text=text)
            log.info("后台开户接口断言通过！")
            # 三方开户
            data = html_parser(result)
            result = self.session.post(url=data[0], data=data[1])
            print("三方开户结果为：", result.text)
            common_assert(result, text=text2)
            log.info("三方开户接口执行结果为:{}".format(result.text))
        except Exception as e:
            log.error(e)
            raise
